package makeorder;

//CONTROLLER 

import guis.FunctionalControls;

import java.util.Date;

import org.eclipse.swt.widgets.Composite;

import application.UserData;
import dao.DataLoader;
import datacontainers.Dish;
import datacontainers.Menu;
import datacontainers.Order;

public class MakeOrderControls implements FunctionalControls,
        MakeOrderInterface {

	private Menu menu;

	private DataLoader dao;
	private MakeOrderGUI gui;
	private OrderMaker orderMaker;

	public MakeOrderControls(Composite tshell, DataLoader dao) {

		gui = new MakeOrderGUI(this, tshell);

		this.dao = dao;
		menu = dao.getMenu();
		orderMaker = new OrderMaker();
		gui.showMenu(menu);

		if (dao.getMenu().getActualDate().after(new Date())
		        & (!dao.checkMenuEditing())) {
			gui.setMenuState("Menu is actual!");
			gui.setMenuEnable(true);
			gui.setMenuDate(dao.getMenu().getActualDate());
		}

		if ((dao.checkMenuEditing())) {
			gui.setMenuState("Menu not actual!\n Is editing!");
		}

		for (Order o : dao.getOrders(UserData.getPCName())) {
			gui.addOrder(o.getDate());
		}

	}

	void disposeControls() {
		//
	}

	void loadOrder(Order order) {

	}

	public void incPosition(Dish dish) {
		orderMaker.inc(dish);
		gui.applyOrderToMenu(orderMaker.getOrder());
		gui.setOrderSumm(orderMaker.getOrderSumm());
	}

	@Override
	public void close() {
		gui.close();
	}

	@Override
	public void sendOrder() {
		Order order = orderMaker.getOrder();
		if (order.getItems().size() != 0) {
			dao.sendOrder(orderMaker.getOrder());
		}
	}

	@Override
	public void delPosition(Dish dish) {
		orderMaker.del(dish);
		gui.applyOrderToMenu(orderMaker.getOrder());
		gui.setOrderSumm(orderMaker.getOrderSumm());
	}

	@Override
	public void loadOldOrder(Date date) {
		for (Order o : dao.getOrders(UserData.getPCName())) {
			if (o.getDate().equals(date)) {
				gui.applyOrderToMenu(o);
			}
		}
	}

}
